using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
/// <summary>
/// 对于大量通过类型回调. 比如100个npc的扣血回调,需要区分对象的
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class EventMapSO<T> : SerializableScriptableObject 
{
    private  Dictionary<int, UnityAction<T>> _keyValuePairs =new Dictionary<int, UnityAction<T>>();

    public void Clear()
    {
        _keyValuePairs.Clear();
    }
#if UNITY_EDITOR && ODIN_INSPECTOR
    [Sirenix.OdinInspector.Button]
#endif
    public void RegistEvent(object obj,UnityAction<T> unityAction)
    {
        var key = obj.GetHashCode();
        if (_keyValuePairs.ContainsKey(key))
        {
            _keyValuePairs[key] += unityAction;
        }
        else
        {
        _keyValuePairs.Add(key, unityAction);
        }
    }
    public void UnRegistEvent(object obj, UnityAction<T> unityAction)
    {
        var key = obj.GetHashCode();
        if (_keyValuePairs.ContainsKey(key))
        {
            _keyValuePairs[key] -= unityAction;
        }
        else
        {
   
        }
    }
    public void RaiseEvent(object obj,T value)
    {
        if(_keyValuePairs.TryGetValue(obj.GetHashCode(),out var action))
        {
            action?.Invoke(value);
        }

    }
}

[CreateAssetMenu(menuName = "Events/int Map Channel")]
public class IntEventMap : EventMapSO<int>
{

}